# @Time : 2024/8/3 21:01
# @Author : Adam
# @Version : v1.0
# @IDE : PyCharm
# @Project : tracer
import time
import collections
import datetime
from django.shortcuts import render
from django.db.models import Count
from web import models
from django.http import JsonResponse


def dashboard(request, project_id):
    """概览"""
    # 问题数据处理
    status_dict = collections.OrderedDict()  # 有序字典
    for key, text in models.Issues.status_choices:
        print(key, text)
        status_dict[key] = {'text': text, 'count': 0}
    issues_data = models.Issues.objects.filter(project_id=project_id).values('status').annotate(ct=Count('id'))
    for item in issues_data:
        status_dict[item['status']]['count'] = item['ct']

    # 项目成员
    user_list = models.ProjectUser.objects.filter(project_id=project_id).values_list('user_id', 'user__username')

    # 最近的10个问题
    top_ten = models.Issues.objects.filter(project_id=project_id, assign__isnull=False).order_by('-id')[0:10]

    context = {
        'status_dict': status_dict,
        'user_list': user_list,
        'top_ten': top_ten
    }

    return render(request, 'dashboard.html', context)


def issues_chart(request, project_id):
    """在概览页面生成highcharts所需的数据"""
    today = datetime.datetime.now().date()

    date_dict = collections.OrderedDict()
    for i in range(0, 30):
        date = today - datetime.timedelta(days=i)
        date_dict[date.strftime('%Y-%m-%d')] = [time.mktime(date.timetuple()) * 1000, 0]

    # 最近30天，每天创建的问题的数量
    # 去数据库查询最近30天的所有的数据 & 根据日期每天都分组

    # select xxxx,1 as ctime from xxxx
    # select id,name,email from table;
    # select id,name, strftime("%Y-%m-%d",create_datetime) as ctime from table;
    # "DATE_FORMAT(web_transaction.create_datetime,'%%Y-%%m-%%d')" mysql
    result = models.Issues.objects.filter(project_id=project_id,
                                          create_datetime__gte=today - datetime.timedelta(days=30)).extra(
        select={'ctime': 'strftime("%%Y-%%m-%%d",web_issues.create_datetime)'}).values('ctime').annotate(ct=Count('id'))
    print(result)
    # <QuerySet [{'ctime': '2024-07-31', 'ct': 1}, {'ctime': '2024-08-01', 'ct': 1}, {'ctime': '2024-08-05', 'ct': 1}]>

    for item in result:
        date_dict[item['ctime']][1] = item['ct']

    return JsonResponse({'status': True, 'data': list(date_dict.values())})
